package com.linln.admin.userules;

import com.alibaba.fastjson.JSONObject;
import com.linln.common.utils.DateUtil;
import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.activityticket.service.ActivityTicketService;
import com.linln.modules.area.domain.Area;
import com.linln.modules.area.service.AreaService;
import com.linln.modules.shopsService.service.ShopsService;
import com.linln.modules.userule.domain.UseRule;
import com.linln.modules.userule.service.UseRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.userules
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.userules
 * @Description: TODO
 * @date Date : 2020年12月28日 14:57
 */
@Slf4j
@Controller
@RequestMapping("useRules")
public class UseRulesController {
    @Autowired
    private UseRuleService useRuleService;
    @Autowired
    private ShopsService shopsService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ActivityTicketService activityTicketService;

    @GetMapping("index")
    public String index() {
        return "/useRules/index";
    }

    @GetMapping()
    @ResponseBody
    public PageResult<UseRule> getUseRuleList(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String searchName) {
        return useRuleService.getUseRuleList(page, limit, searchName);
    }


    @PostMapping
    @ResponseBody
    public JsonResult addUseRules(@RequestParam() Map map) {
        try {
            if (useRuleService.addUseRule(createUseRule(map))) {
                return JsonResult.ok("保存成功");
            }
            return JsonResult.error("保存失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("保存失败");
        }
    }

    @PutMapping
    @ResponseBody
    public JsonResult updateUseRules(@RequestParam() Map map) {
        try {
            if (useRuleService.updateUseRule(createUseRule(map))) {
                return JsonResult.ok("更新成功");
            }
            return JsonResult.error("更新失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("更新失败");
        }
    }

    @ResponseBody
    @RequestMapping("/shopId/{shopId}")
    public JsonResult getShopNameByid(@PathVariable("shopId") String shopId) {
        return JsonResult.ok().put("shopName", shopsService.getById(shopId).getShopName());
    }

    @DeleteMapping("/{useRuleId}")
    @ResponseBody
    public JsonResult delUseRules(@PathVariable("useRuleId") String useRuleId) {
        try {
            UseRule byId = useRuleService.getById(useRuleId);
            if (byId.getCanDelete()) {
                return JsonResult.error("删除失败,该规则不能删除");
            }
            if (activityTicketService.useRuleIsItAssociatedByActicityTicket(useRuleId)) {
                return JsonResult.error("删除失败,已被活动券关联");
            }
            if (useRuleService.delUseRule(useRuleId)) {
                return JsonResult.ok("删除成功");
            }
            return JsonResult.error("删除失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("删除失败");
        }
    }


    @ResponseBody
    @RequestMapping("areaId/{areaId}")
    public JsonResult getAreaNameByAreaId(@PathVariable("areaId") String areaId) {
        Area byId = areaService.getById(areaId);
        return JsonResult.ok().put("areaName", byId.getName());
    }

    private UseRule createUseRule(Map<String, Object> map) {
        System.out.println(map);
        UseRule useRule = new UseRule();
        if (map.get("useRuleId") != null) {
            useRule.setUseRuleId(String.valueOf(map.get("useRuleId")));
            map.remove("useRuleId");
            useRule.setUseRuleCreateDate(DateUtil.formatDate(new Date()));
        }
        useRule.setUseRuleName(String.valueOf(map.get("useRuleName")));
        map.remove("useRuleName");
        useRule.setUseRuleDescribe(String.valueOf(map.get("useRuleDescribe")));
        map.remove("useRuleDescribe");
        String props = JSONObject.toJSONString(map);

//        String shopType = map.get("shopType");
//        String shopId = map.get("shopId");
//        String rearId = map.get("rearId");
        useRule.setUseRuleProps(props);

        return useRule;
    }


}
